// mircoApp/numberCollection/classify/classify.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    pluginType:'collection-center',
    isLoadMore: true,
    totalPage: 0,
    query:{
      parent_id:'', //一级分类id（不传就返回一级分类）
      is_home:'',
    },
    params:{
      search:{
        keyword:'',
        parentid:'',
        childid:'',
      },
      order_field:'',
      order_by:'',
      page: 1
    },
    storeScroll: "",
    classifyScroll: "",
    classifyIndex:0,
    activeName:[],
    classifyArr:[],
    showTitle:[],
    secondaryId:'',
    yzSupplyLease:false,
    secondArr:[
      {
        id:'',
        name:'全部'
      }
    ],
    classifyList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.category();
  },
  goodsNav() {
    //mircoApp/numberCollection/collectionUser/collectionUser
    wx.navigateTo({
      url: '/mircoApp/numberCollection/collectionUser/collectionUser',
    });
  },
  sortOut(e) {
    let detail = e.detail;
    // this.hidebox();
    if (this.data.classifyList.length == 0) {
      return;
    }
    this.setData({
      'params.page':1,
      'params.order_field':detail.order_field,
      'params.order_by':detail.order_by
    })
    this.categoryData();
  },
  letterOn() { //推荐分类
    let secondArr = [
      {
        id:'',
        name:'全部'
      }
    ];
    this.setData({
      secondArr,
      showTitle:[],
      classifyIndex:-1,
      'query.parent_id':'',
      'query.is_home':'',
      'params.search.parentid':'',
      'params.search.childid':'',
      secondaryId:''
    });
    this.category2();
    this.categoryData();
  },
  chooseSecondary(event) { //二级分类
    let {id,level} = event.currentTarget.dataset.item;
    if(level == 1) {
      this.data.params.search.parentid = id;
      this.data.params.search.childid = '';
    } else {
      this.data.params.search.parentid = '';
      this.data.params.search.childid = id;
    }
    this.setData({
      'params.search.childid':this.data.params.search.childid,
      'params.search.parentid':this.data.params.search.parentid,
      secondaryId:id,
      'params.page':1,
      showTitle:this.data.secondArr,
      activeName:[]
    })
    this.categoryData();
  },
  classifyOnText(event) { //切换分类
    let {index,id} = event.currentTarget.dataset;
    let secondArr = [
      {
        id:'',
        name:'全部'
      }
    ];
    this.setData({
      secondArr,
      classifyIndex:index,
      'params.search.childid':'',
      'params.search.parentid':id,
      'query.parent_id':id,
      'params.page':1,
      'query.is_home':0,
      showTitle:[],
      secondaryId:''
    })
    this.category2();
    this.categoryData();
  },
  initData() {
    this.params.page = 1;
    this.query.parent_id = '';
    this.params.search.parentid = '';
    this.params.search.childid = '';
    this.query.parentid = '';
    this.classifyIndex = 0;
  },
  category() { //分类信息
    let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.goods.category.index");
    app._postNetWork({
      url: urlStr,
      data: this.data.query,
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          let classifyArr = data.category_list;
          if(classifyArr) {
            this.setData({
              classifyArr,
              'params.search.parentid':data.category_list[0].id,
              'query.parent_id':data.category_list[0].id,
            })
            this.categoryData();
            this.category2();
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  category2() { //分类信息
    let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.goods.category.index");
    app._postNetWork({
      url: urlStr,
      data: this.data.query,
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          let list = data.category_list;
          this.setData({
            secondArr:[...this.data.secondArr,...list],
            showTitle:[...this.data.secondArr,...list]
          })
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  categoryData() {
    let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.goods.goods.index");
    app._postNetWork({
      url: urlStr,
      data:this.data.params,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.goods_list?.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            classifyList: data.goods_list.data,
          })
        } else {
          this.data.params.page = this.data.params.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.params.page >= this.data.totalPage) {
      return;
    } else {
      this.data.params.page = this.data.params.page + 1;
      let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.goods.goods.index");
      app._postNetWork({
        url: urlStr,
        data: this.data.params,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.goods_list?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              classifyList : this.data.classifyList.concat(list),
            })
          } else {
            this.data.params.page = this.data.params.page -1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  titleChange(event) {
    let detail = event.detail;
    if(detail[0] == '1') {
      this.data.showTitle = [];
    } else{
      this.data.showTitle = this.data.secondArr;
    }
    this.setData({
      activeName: event.detail,
      showTitle:this.data.showTitle
    });
  },
  cloneCollapse() {
    this.setData({
      activeName: [],
    });
  },
  searchTo() {
    wx.navigateTo({
      url: '/packageB/member/category/search_v2/search_v2?tag=collection-center',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})